<template>
  <div class="app-container">

    <el-form ref="param" :inline="true" :model="param" class="demo-form-inline">
      <el-form-item prop="name">
        <el-input v-model="param.name" placeholder="角色名称" />
      </el-form-item>

      <el-form-item prop="createDate">
        <el-date-picker
          v-model="param.createDate"
          unlink-panels
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" round @click="onSearch">查询</el-button>
        <el-button icon="el-icon-delete" round @click="onClear('param')">清空</el-button>
        <el-button type="success" icon="el-icon-document-add" round @click="add">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        width="50"
      />
      <el-table-column
        property="name"
        label="角色名称"
      />
      <el-table-column
        label="创建时间"
        width="180"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            round
            type="primary"
            size="mini"
            icon="el-icon-view"
            @click="handleShow(scope.$index, scope.row)"
          >
            查看
          </el-button>
          <el-button
            round
            type="warning"
            size="mini"
            icon="el-icon-edit"
            @click="update(scope.$index, scope.row)"
          >
            修改
          </el-button>
          <el-button
            round
            type="danger"
            size="mini"
            icon="el-icon-delete-solid"
            @click="deleteRole(scope.$index, scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <br>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next"
      :page-sizes="[10, 20, 30, 50, 100]"
      :current-page="param.pageNum"
      :total="page.total"
      @current-change="pageNumChange"
      @size-change="pageSizeChange"
    />
    <role-dialog ref="roleDialog" :tree-data="treeData" :role-form="roleForm" @addRole="addRole" />

    <el-dialog
      title="角色详情"
      :visible.sync="dialogVisible"
    >
      <el-row :span="24">
        <el-col :span="24">角色名称 : {{ roleInfo.name }}</el-col>
      </el-row>
      <el-row :span="24">
        <el-col :span="3">用户权限 :</el-col>
        <el-col :span="21">
          <el-tree
            :data="roleInfo.treeData"
            :props="defaultProps"
          />
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { findList, findAllMenuList, addOrUpdate, findSysRoleById, deleteSysRoleById } from '@/api/role'
import RoleDialog from './component/RoleDialog.vue'
export default {
  components: {
    RoleDialog
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      dialogVisible: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      param: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        createDate: []
      },
      page: {
        total: 0
      },
      list: [],
      listLoading: true,
      treeData: [],
      roleForm: {
        id: '',
        name: '',
        menuIds: []
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      },
      roleInfo: {
        name: '',
        treeData: []
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.listLoading = true
      findList(this.param).then(res => {
        this.list = res.data.list
        this.page.total = res.data.total
        this.listLoading = false
      })
    },
    add() {
      findAllMenuList().then(res => {
        this.treeData = res.data
      })
      this.$refs.roleDialog.addDialog = true
    },
    addRole(roleFormData) {
      addOrUpdate(roleFormData).then(res => {
        this.$message({
          message: res.msg,
          type: 'success'
        })
        this.$refs.roleDialog.addDialog = false
        // 刷新页面
        this.param.pageNum = 1
        this.init()
      })
    },
    update(index, row) {
      findSysRoleById({ id: row.id }).then(res => {
        this.roleForm = res.data.role
        this.treeData = res.data.treeData
        this.$refs.roleDialog.addDialog = true
      })
    },
    deleteRole(index, row) {
      this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSysRoleById({ id: row.id }).then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
          this.init()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleShow(index, row) {
      this.roleInfo.treeData = []
      this.roleInfo.name = row.name
      findAllMenuList().then(res => {
        this.roleInfo.treeData = res.data
      })
      this.dialogVisible = true
    },
    onSearch() {
      this.param.pageNum = 1
      this.init()
    },
    onClear(formName) {
      this.$refs[formName].resetFields()
    },
    pageNumChange(index) {
      this.param.pageNum = index
      this.init()
    },
    pageSizeChange(index) {
      this.param.pageSize = index
      this.init()
    }
  }
}
</script>
